/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.util;

import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.Optional;

/**
 * 显示相关工具类, 提供vp-px转换,屏幕大小等方法
 *
 * @since 2021-03-29
 */
public final class DisplayUtils {
    private DisplayUtils() {
    }

    /**
     * 获取屏幕宽度
     *
     * @param context 上下文
     * @return 屏幕宽度
     */
    public static int getDisplayWidthInPx(Context context) {
        return (int) getDisplayPoint(context).orElse(new Point()).getPointX();
    }

    /**
     * 获取屏幕高度，不包含状态栏的高度
     *
     * @param context 上下文
     * @return 屏幕高度，不包含状态栏的高度
     */
    public static int getDisplayHeightInPx(Context context) {
        return (int) getDisplayPoint(context).orElse(new Point()).getPointY();
    }

    /**
     * 获取屏幕的Point, 可获取屏幕宽高属性
     *
     * @param context 上下文
     * @return 可获取屏幕宽高属性Point
     */
    public static Optional<Point> getDisplayPoint(Context context) {
        return DisplayManager.getInstance().getDefaultDisplay(context)
            .map(display -> {
                Point point = new Point();
                display.getSize(point);
                return Optional.of(point);
            }).orElse(null);
    }

    /**
     * vp转像素
     *
     * @param context 上下文
     * @param vp vp值
     * @return px 像素值
     */
    public static int vp2px(Context context, float vp) {
        return DisplayManager.getInstance().getDefaultDisplay(context)
            .map(display -> (int) (display.getAttributes().densityPixels * vp))
            .orElse(0);
    }

    /**
     * px转vp
     *
     * @param context 上下文
     * @param px 像素
     * @return 对应的vp值
     */
    public static int px2vp(Context context, float px) {
        return DisplayManager.getInstance().getDefaultDisplay(context)
            .map(display -> (int) (px / display.getAttributes().densityPixels))
            .orElse(0);
    }

    /**
     * 获取屏幕的宽度，括状态栏
     *
     * @param context
     * @return 宽度
     */
    public static int getDisplayWidth(Context context) {
        return getDeviceAttr(context).getRealAttributes().width;
    }

    /**
     * 获取屏幕的宽度，不包括状态栏
     *
     * @param context
     * @return 宽度
     */
    public static int getAttributesWidth(Context context) {
        return getDeviceAttr(context).getAttributes().width;
    }

    /**
     * 获取当前设备属性
     *
     * @param context
     * @return Display
     */
    public static Display getDeviceAttr(Context context) {
        Optional<Display> optional = DisplayManager.getInstance().getDefaultDisplay(context);
        return optional.get();
    }
}
